import numpy as np



def new_array(array,rate):  #array是定义好的全1矩阵，rate是矩阵中每个值取0的概率
    '''按照数组模板生成对应的 0-1 矩阵'''
    zeros_num = int(array.size * rate)  #根据0的比率来得到 0的个数
    new_array = np.ones(array.size)  #生成与原来模板相同的矩阵，全为1
    new_array[:zeros_num] = 0  #将一部分换为0
    np.random.shuffle(new_array)  #将0和1的顺序打乱
    re_array = new_array.reshape(array.shape)  #重新定义矩阵的维度，与模板相同
    return re_array


arr = np.ones(225)  # 生成一维数组arr，值默认为1
arr2 = arr.reshape(15, 15)  # 将数组array变形成15*15的矩阵arr2
new_arr = new_array(arr2,rate=0.5)  # 设定每个数字变为0的概率 rate

print(new_arr)  # 输出生成的初始0，1矩阵

# 生成含有225个值初始列表，用于存放团簇值的大小
N = [[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
     [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
     [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
     [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
     [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
     [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
     [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
     [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
     [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
     [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
     [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
     [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
     [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
     [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
     [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]]

N = np.array(N)

k = -1 # k是上面列表N的索引控制值
l = 0
def up(i,j): # 负责控制团簇向上方向遍历
    global l
    global b
    if i == -16:
        i = 14
    if new_arr[i][j] == 0:
        N[l][k] = N[l][k] + 1
        new_arr[i][j] = 2
        e = i
        f = j-1
        left(e,f)
        g = i
        h = j+1
        right(g,h)
        i = i-1
        up(i,j)
    else:
        N[l][k] = N[l][k]


def down(i,j): # 负责控制团簇向下方向遍历
    global k
    global l
    if i == 15 :
        i = -15
    if new_arr[i][j] == 0:
        N[l][k] = N[l][k] + 1
        new_arr[i][j] = 2
        e = i
        f = j-1
        left(e,f)
        g = i
        h = j+1
        right(g,h)
        i = i+1
        down(i,j)
    else:
        N[l][k] = N[l][k]


def left(i,j): # 负责控制团簇向左方向遍历
    global k
    global l
    if j == -16:
        j = 14
    if new_arr[i][j] == 0:
        N[l][k] = N[l][k] + 1
        new_arr[i][j] = 2
        a = i-1
        b = j
        up(a,b)
        c = i+1
        d = j
        down(c,d)
        j = j-1
        left(i,j)
    else:
        N[l][k] = N[l][k]


def right(i,j): # 负责控制团簇向右方向遍历
    global k
    global l
    if j == 15:
        j = -15
    if new_arr[i][j] == 0:
        N[l][k] = N[l][k] + 1
        new_arr[i][j] = 2
        a = i-1
        b = j
        up(a,b)
        c = i+1
        d = j
        down(c,d)
        j = j+1
        right(i,j)
    else:
        N[l][k] = N[l][k]




def Sagiri(): # 团簇遍历的总控制函数，负责综合矩阵中四个方向的遍历，确保矩阵中的每个值都得到遍历
    global k
    global l
    for i in range(0,15,1):
        for j in range(0,15,1):
            l = i
            k = j
            if new_arr[i][j] == 0:
                N[l][k] = N[l][k] + 1
                new_arr[i][j] = 2
                a = i-1
                b = j
                up(a,b)
                c = i+1
                d = j
                down(c,d)
                e = i
                f = j-1
                left(e,f)
                g = i
                h = j+1
                right(g,h)

            if new_arr[i][j] == 1: # 当矩阵中值为1时，不计入团簇，并结束该次循坏
                continue
Sagiri()

# 计算最大值在矩阵中的行列坐标

max = np.where(N == np.max(N))




print(N)
print('new_arr',new_arr)



print(max)




